﻿using System;
using System.Configuration;
using System.IO;
using System.Web;
using nkstdgia.main.report;
using wsRK.ConvertionHelper;
using wsRK.WindowsUtility;

namespace nkstdgia
{
    public class FileDownload : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string strType = context.Request.QueryString["type"] == null ? "" : context.Request.QueryString["type"].ToString();
                string id = context.Request.QueryString["id"] == null ? "" : context.Request.QueryString["id"].ToString();
                ExcelCommand excelCmd = null;
                byte[] byteArr = null;
                switch (strType)
                {
                    case "exportptd":
                        excelCmd = new ExcelCommand();
                        byteArr = excelCmd.exportToExcel(ConvertHelper.toInt(id));
                        if (byteArr != null)
                        {
                            // save
                            String fileName = ConfigurationManager.AppSettings["ExcelExportFileName"] + id;

                            context.Response.ClearContent();
                            context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");
                            context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                            context.Response.BinaryWrite(byteArr);
                            context.Response.Flush();
                            context.Response.Close();
                        }
                        break;
                    default:
                        if (!string.IsNullOrEmpty(strType) && File.Exists(strType))
                        {
                            byteArr = WindowsFileAccess.readFileToByte(strType);
                            string[] filePaths = Directory.GetFiles(context.Server.MapPath(ConfigurationManager.AppSettings["ExcelTongHopExportDesPath"]));
                            foreach (string item in filePaths)
                            {
                                FileInfo f = new FileInfo(item);
                                if (f.CreationTime < DateTime.Today.AddDays(-1))
                                {
                                    File.Delete(item);
                                }
                            }
                            //context.Session["exportlist"] = null;
                            if (byteArr != null)
                            {
                                // save
                                String fileName = ConfigurationManager.AppSettings["ExcelExportFileName"] + "Total_excel";

                                context.Response.ClearContent();
                                context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");
                                context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                                context.Response.BinaryWrite(byteArr);
                                context.Response.Flush();
                                context.Response.Close();
                            }
                        }
                        
                        break;
                }

            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
